﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using Shop.Core.Domain.Common;
using Shop.Core.Domain.Customers;
using Shop.Core.Domain.Payments;
using Shop.Core.Domain.Shipping;
using Shop.Core.Events;
using Shop.Infrastructure.Containers;

namespace Shop.Core.Domain.Orders
{
    /*
     * ID       ：yyMMddXXXXXX
     * yy       ：年；MM：月；dd：日
     * XXXXXX   ：六位循环递增随机数字
     * 多个写库 ：yy前增加两位设置成当前库唯一编号。
     *          ：读库时仓储层会自动选择是哪个库，
     *          ：写库时仓储层会自动选择一台空闲的库。
     * 读写分离 ：目前只支持一台写库。
     *          ：多个写库待定。
     */
    [Serializable]
    public partial class Order : BaseEntity<string>
    {
        public Order()
        {
            this.CreatedOn = DateTime.UtcNow;
            this.OrderType = Orders.OrderType.Buy;
            this.PaymentLockStatus = Payments.PaymentLockStatus.None;
            this.PaymentMethod = Payments.PaymentMethod.AliPay;
            this.PaymentStatus = PaymentStatues.PaymentPending;
            this.ProduceLockStatus = Orders.ProduceLockStatus.None;
            this.ShippingMethod = Shipping.ShippingMethod.ShunFeng;
            this.ShippingStatus = Shipping.ShippingStatus.ShipPending;
            this.FlowNode = FlowNodeFactory.GetFlowNode(this.OrderStatus);
        }

        #region Status

        public virtual PaymentStatues PaymentStatus
        {
            get;
            set;
        }

        public virtual ShippingStatus ShippingStatus
        {
            get;
            set;
        }

        public virtual ProduceLockStatus ProduceLockStatus
        {
            get;
            set;
        }

        public virtual PaymentLockStatus PaymentLockStatus
        {
            get;
            set;
        }

        public virtual OrderStatus OrderStatus
        {
            get
            {
                if (this.PaymentLockStatus != PaymentLockStatus.None)
                {
                    return (OrderStatus)this.PaymentLockStatus;
                }
                if (this.ProduceLockStatus != ProduceLockStatus.None)
                {
                    return (OrderStatus)this.ProduceLockStatus;
                }
                //如果是先货后款订单，下单后直接就是等待发货状态
                if (this.ShippingStatus != Shipping.ShippingStatus.ShipPending
                    || this.PaymentMethod == PaymentMethod.DeliveredAndCard
                    || this.PaymentMethod == PaymentMethod.DeliveredAndCash)
                {
                    return (OrderStatus)this.ShippingStatus;
                }
                return (OrderStatus)this.PaymentStatus;
            }
        }

        /// <summary>
        /// 不允许直接赋值
        /// </summary>
        protected virtual IOrderFlowNode FlowNode
        {
            get;
            set;
        }

        #endregion

        #region Money

        /// <summary>
        /// 
        /// </summary>
        public virtual Money CapturedAmount
        {
            get;
            protected set;
        }

        public virtual Money GoodsAmount
        {
            //{
            //    if (this.Products != null)
            //    {
            //        decimal m = 0;
            //        foreach (OrderProduct op in this.Products)
            //        {
            //            m += op.TotalAmount.Value;
            //        }
            //        this.goodsTotal = new Money(m);
            //    }
            //    return this.goodsTotal;
            //}
            get;
            protected set;
        }

        public virtual Money ShippingFee
        {
            get;
            set;
        }

        public virtual Money DiscountAmount
        {
            get;
            set;
        }

        public virtual Money GrandTotal
        {
            get
            {
                return this.GoodsAmount + this.ShippingFee - this.DiscountAmount;
            }
        }

        /// <summary>
        /// 存储的是正数
        /// </summary>
        public virtual Money RefundedAmount
        {
            get;
            set;
        }

        /// <summary>
        /// 实际支付金额。= this.CapturedAmount - this.RefundedAmount
        /// </summary>
        public virtual Money ActualCapturedAmount
        {
            get
            {
                return this.CapturedAmount - this.RefundedAmount;
            }
        }

        #endregion

        #region Misc

        /// <summary>
        /// 用户Id
        /// </summary>
        public int CustomerId
        {
            get;
            protected set;
        }

        /// <summary>
        /// 是否已经逻辑删除
        /// </summary>
        public bool IsDeleted
        {
            get;
            set;
        }

        /// <summary>
        /// 订单类型
        /// </summary>
        public virtual OrderType OrderType
        {
            get;
            set;
        }

        public virtual PaymentMethod PaymentMethod
        {
            get;
            set;
        }

        public virtual ShippingMethod ShippingMethod
        {
            get;
            set;
        }

        /// <summary>
        /// 订单创建时间
        /// </summary>
        public virtual DateTime CreatedOn
        {
            get;
            internal set;
        }

        /// <summary>
        /// 上次修改时间戳
        /// </summary>
        public virtual DateTime ModifiedOn
        {
            get;
            set;
        }

        private DateTime? modifyingOn = null;
        /// <summary>
        /// 此次修改时间戳
        /// </summary>
        public virtual DateTime ModifyingOn
        {
            get
            {
                if (this.modifyingOn == null)
                {
                    this.modifyingOn = DateTime.UtcNow;
                }
                return this.modifyingOn.Value;
            }
            set
            {
                this.modifyingOn = value;
            }
        }

        /// <summary>
        /// 创建者：系统或者后台客服
        /// </summary>
        public virtual int CreatedBy
        {
            get;
            set;
        }

        /// <summary>
        /// 客人填写的订单备注，没有任何业务逻辑。
        /// </summary>
        public string Memo
        {
            get;
            set;
        }
        #endregion

        #region Navigation properties
        public virtual Address ShippingAddress
        {
            get;
            protected set;
        }

        private Customer customer = null;
        public virtual Customer Customer
        {
            get
            {
                if (this.customer == null)
                {
                    this.customer = Container.Current.Resolve<IEventPublisher>().EntityRequired<Customer, int>(this.CustomerId);
                }
                return this.customer;
            }
            protected set
            {
                this.customer = value;
                this.CustomerId = this.customer.Id;
            }
        }

        IList<OrderProduct> products = null;
        public virtual IList<OrderProduct> Products
        {
            get
            {
                if (this.products == null)
                {
                    this.products = new List<OrderProduct>();
                }
                return this.products;
            }
            set
            {
                this.products = value;
            }
        }
        #endregion
    }
}
